package com.reggie.employee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.employee.entity.Employee;
import com.reggie.configuration.common.Result;
import com.reggie.configuration.common.tableList;
import com.reggie.employee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 用户登陆操作
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    Result<Employee> Login(HttpServletRequest request, @RequestBody Employee employee) {
        final String username = employee.getUsername();
        //因为后台数据密码是加密后的，所以要用这串命令进行加密一下
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //lambda的wrapper必须要加泛型
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Employee::getUsername, username);
        final Employee one = employeeService.getOne(lambdaQueryWrapper);

        if (one == null) {
            return Result.error("暂无该用户信息");
        } else if (!one.getPassword().equals(password)) {
            return Result.error("登陆失败，请重试");
        } else if (one.getStatus() == 0) {
            return Result.error("该用户已停用");
        } else {
            log.info(one.getUsername() + "已登录");
            request.getSession().setAttribute("employee", one.getId());
            return Result.success(one);
        }
    }

    /**
     * 用户登出操作
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前登录员工的id
        log.info("{}已退出登录", request.getSession().getAttribute("employee"));
        request.getSession().invalidate();
        return Result.success("退出成功");
    }

    /**
     * 页码查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<tableList> selectByCondition(Integer page, Integer pageSize, String name) {
        return employeeService.selectByCondition(page, pageSize, name);
    }

    /**
     * 添加新员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public Result<String> InsertUser(@RequestBody Employee employee) {
        if (employee.getPhone().toString().length() != 11) {
            return Result.error("您输入的手机号长度有误，请重新输入");
        }
        if (employee.getIdNumber().toString().length() != 18) {
            return Result.error("您输入的身份证号长度有误，请重新输入");
        }
        log.info("新员工信息为{}", employee);

        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.saveOrUpdate(employee);
        return Result.success("添加成功");
    }

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable Long id) {
        if (id == null) {
            log.info("ID为空");
            return Result.error("ID为空");
        }
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return Result.success(employee);
        }
        return Result.error("没有查询到对应员工信息");
    }

    @PutMapping
    public Result<String> updateUser(@RequestBody Employee employee) {
        log.info("修改后的员工信息{}", employee.toString());

        employeeService.updateById(employee);
        return Result.success("修改成功");
    }

    /**
     * 修改员工状态
     * @param employee
     * @param request
     * @return
     */
    /*@PutMapping
    public Result<String> setStatus(@RequestBody Employee employee,HttpServletRequest request){
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
        employeeService.saveOrUpdate(employee);
        return Result.success("OK");
    }*/
}
